import tkinter as tk
import common
import requests

class NetworkPanel():
    __static_obj = None
    
    def __init__(self, right_panel) -> None:
        self.network_panel = tk.Frame(right_panel, borderwidth=1, relief=tk.SOLID, bg="red")
        #self.network_panel.place(x=0, y=0, width=100, height=200)
        #self.network_panel.pack(fill=tk.BOTH, expand=True)
        
        # 添加不可修改的标签
        self.network_label = tk.Label(self.network_panel, text="网络测试", font=("Arial", 14, "bold"))
        self.network_label.pack(side=tk.TOP, padx=10, pady=10)
        
        self.send_frame = tk.Frame(self.network_panel)
        #self.send_frame.pack(side=tk.TOP, padx=10, pady=10)
        self.send_frame.place(x=0, y=60, width=right_panel.winfo_width(), height=100)
        #self.send_entry = tk.Entry(self.send_frame)
        #self.send_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, pady=10)
        self.send_txt = tk.Text(self.send_frame)
        self.send_txt.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, pady=1)
        self.send_button = tk.Button(self.send_frame, text="发送", command=self.send_network_data)
        self.send_button.pack(side=tk.LEFT, padx=10)
        
        self.recv_frame = tk.Frame(self.network_panel)
        #self.send_frame.pack(side=tk.TOP, padx=10, pady=10)
        self.recv_frame.place(x=0, y=130, width=right_panel.winfo_width(), height=500)
        self.recv_txt = tk.Text(self.recv_frame, state=tk.DISABLED)
        self.recv_txt.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, pady=10)
        
        self.network_panel.pack_forget()
        common.right_panel_register("network", self.network_panel)
        print("network panel init")
    
    @classmethod
    def instance(cls, *args, **kwargs):
        if not cls.__static_obj:
            cls.__static_obj = cls(*args, **kwargs)
        return cls.__static_obj
    
    def show_network_panel(self):
        common.right_panel_show("network")
    
    def send_network_data(self):
        '''
        print(self.send_entry.get())
        self.recv_txt.config(state=tk.NORMAL)
        self.recv_txt.delete("1.0", tk.END)
        self.recv_txt.insert(tk.END, self.send_entry.get())
        self.recv_txt.config(state=tk.DISABLED)
        '''
        url = self.send_txt.get("1.0", tk.END)
        if url:  # 确保输入框不为空
            self.normal_recv_txt()
            #self.update_recv_txt(f"请求 URL: {url}\n")
            self.recv_txt.insert(tk.END, f"请求 URL: {url}\n")
            print(url)
            try:
                response = requests.get(url, allow_redirects=False)
                #response.raise_for_status()  # 检查请求是否成功
                self.recv_txt.insert(tk.END, f"响应:\n")
                for key, value in response.headers.items():
                    self.recv_txt.insert(tk.END, f"{key}: {value}\r\n")
                self.recv_txt.insert(tk.END, f"\r\n{response.text}")
            except requests.exceptions.RequestException as e:
                self.disable_recv_txt(f"请求失败: {e}\n")
            finally:
                #self.send_entry.delete(0, tk.END)  # 清空输入框
                self.disable_recv_txt()
                pass
        pass

    def normal_recv_txt(self):
        self.recv_txt.config(state=tk.NORMAL)
        self.recv_txt.delete("1.0", tk.END)  # 清空文本
    
    def disable_recv_txt(self):
        self.recv_txt.config(state=tk.DISABLED)
        self.recv_txt.see(tk.END)  # 滚动到最新内容
    